package com.zmn.oms.zmn.business.impl.grab;

import com.alibaba.fastjson.JSONObject;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.constant.MqTopicConsts;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import com.zmn.oms.business.interfaces.work.OrderWorkBService;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.work.modify.must.OrderWorkGrabDTO;
import com.zmn.oms.model.entity.grab.NonGridGrabOrder;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.grab.NonGridGrabOrderService;
import com.zmn.oms.services.interfaces.tag.OrderTagService;
import com.zmn.oms.zmn.business.interfaces.grab.NonGridGrabOrderBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2022/07/27 10:21
 */
@Service
@Slf4j
public class NonGridGrabOrderBServiceImpl implements NonGridGrabOrderBService {

    @Autowired
    private NonGridGrabOrderService nonGridGrabOrderService;
    @Autowired
    private OrderWorkBService orderWorkBService;
    @Resource
    private ZmnMQSender zmnMQSender;

    @Override
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_CHANGE_ASSIGNMENT)
    public void modifyGrabType(OrderWorkGrabDTO orderWorkGrabDTO) {

        // 从抢单池出来
        if (Objects.equals(orderWorkGrabDTO.getGrabType(), GlobalConsts.NO)) {
            NonGridGrabOrder nonGridGrabOrder = new NonGridGrabOrder();
            nonGridGrabOrder.setOrderId(orderWorkGrabDTO.getOrderId());
            nonGridGrabOrder.setWorkId(orderWorkGrabDTO.getWorkId());
            nonGridGrabOrder.setValidStatus(GlobalConsts.NO);
            nonGridGrabOrderService.updateByKey(nonGridGrabOrder);

            // 是否结束无网格抢单，结束后扔到队列自动取消
            if (Objects.equals(orderWorkGrabDTO.getEndNonGridGrab(), GlobalConsts.YES)) {
                String key = String.format("auto-cancel-%s", orderWorkGrabDTO.getOrderId());
                JSONObject jb = new JSONObject();
                jb.put("orderId", orderWorkGrabDTO.getOrderId());
                jb.put("workId", orderWorkGrabDTO.getWorkId());
                log.info("发送延时取消消息：tag:{},key:{},message:{}", OmsMqTagConsts.ORDER_WORK_JXJ_DELAYED_CANCEL_TAG, key, jb.toJSONString());
                zmnMQSender.send(MqTopicConsts.ZMN_TOPIC_OMS, OmsMqTagConsts.ORDER_WORK_JXJ_DELAYED_CANCEL_TAG, key, jb.toJSONString());
            }
        }

        // 更新状态
        OrderWork updateOrderWork = new OrderWork();
        updateOrderWork.setOrderId(orderWorkGrabDTO.getOrderId());
        updateOrderWork.setWorkId(orderWorkGrabDTO.getWorkId());
        updateOrderWork.setGrabType(orderWorkGrabDTO.getGrabType());
        orderWorkBService.updateOrderWork(updateOrderWork);
    }
}
